<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input id="text" type="text">
    <input id="box" type="checkbox">记住用户名
    <script>
        var text = document.querySelector('#text');
        var box = document.querySelector('#box');
        if (localStorage.getItem('uname')) {
            text.value = localStorage.getItem('uname');
            box.checked = true;
        }
        box.addEventListener('change', function() {
            if (this.checked) {
                localStorage.setItem('uname', text.value);
            } else {
                localStorage.removeItem('uname');
            }
        })
    </script>
</body>

</html>